'''
Created on 22/12/2009

@author: vinicius
'''

from math import exp,sqrt,hypot
from numpy import matrix
from csv import reader

def euclidiana(self, padrao1, padrao2):
    distancia = 0.0
    for i in range(len(padrao1.atributos)):
        distancia = distancia + pow(padrao1.atributos[i] - padrao2.atributos[i], 2)
    return sqrt(distancia)

def logistic(campoLocalInduzido):
    return 1 / (1 + exp(-campoLocalInduzido))

def erroTotal(vetorErro):
    erroQuadratico = 0.0
    for erro in vetorErro:
        erroQuadratico += pow(erro, 2)
    return 0.5 * erroQuadratico
    
def erroMedioQuadratico(entradas):
    mse = 0.0
    for padrao in entradas:
        mse += padrao.erroTotal
    return (1.0 / (len(entradas))) * mse

def norma(x,p):
    diff = matrix(x) - matrix(p)
    x0 = diff[:,0]
    x1 = diff[:,1]
    y = hypot(x0, x1)
    return y

def valorMinimo(valores):
    valorMinimo = 9999999.9
    i = -1
    for indice in range(len(valores)):
        if valores[indice] < valorMinimo:
            valorMinimo = valores[indice]
            i = indice
    return i

def define_input(fileName):
    input = reader(open(fileName,'r'))
    entradas = []
    labels = []
    for attributes in input:
        atributos = []
        labels.append(attributes[len(attributes)-1])
        for i in range(len(attributes)-1):
            atributos.append(float(attributes[i]))
        entradas.append(atributos)
    return (entradas,labels)